package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Model;

import java.util.Date;

/**
 * Created by Czl on 2015/9/12.
 */
@SuppressWarnings("serial")
public class SystemMessage extends Model<SystemMessage> {

    public static final SystemMessage dao = new SystemMessage();

    /**
     * 根据Id筛选出不必要的字段，不传递
     * @param msgId
     * @return
     */
    public SystemMessage findByMsgId(Long msgId){
        return dao.findFirst("select t.id,t.title,t.create_time,t.msg_type from t_system_message t " +
                "where t.id = ? " +
                "and  t.is_open = 1 " +
                "and t.start_time < now() " +
                "and t.end_time > now() " +
                "order by t.create_time desc ",msgId);
    }

    public void saveMessage(SystemMessage message, String title, int is_open, int is_forever, int msg_type, Date startTime, Date endTime) {
        message.set("title", title);
        message.set("is_open", is_open);
        message.set("is_forever", is_forever);
        message.set("msg_type", msg_type);
        message.set("start_time", startTime);
        message.set("end_time", endTime);
        message.set("create_time", new Date());
        message.remove("id").save();
    }
}
